package com.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.entity.Project;
import com.util.DBHelp;
import com.util.RowMapper;

public class ProjectDao {

	DBHelp<Project> help=new DBHelp<Project>();
	
	public List<Project> getProjects(String id) {
		String sql="SELECT 	id,`name`,`desc`,createtime FROM t_project WHERE id IN(SELECT projectid FROM t_employee_project WHERE employeeid=?) ORDER BY `createtime` DESC";
		return help.executeQueryForList(sql, new Mapper(), id);
	}
	
	private class Mapper implements RowMapper<Project>{

		public Project mapperRow(ResultSet rs) throws SQLException {
			Project project=new Project();
			project.setId(rs.getString("id"));
			project.setCreatetime(rs.getString("createtime"));
			project.setDesc(rs.getString("desc"));
			project.setName(rs.getString("name"));
			
			return project;
		}
		
	}

	public void addProject(Project project) {
		String sql="INSERT INTO t_project(id,`name`,`desc`,createtime) VALUES(?,?,?,?)";
		help.executeSQL(sql, project.getId(),project.getName(),project.getDesc(),project.getCreatetime());
	}
	
	/*public Project getProject(String createtime){
		String sql="SELECT id,`name`,`desc`,createtime FROM t_project WHERE createtime=?";
		return help.executeQueryForObject(sql, new Mapper(), createtime);
	}*/

	public Project getProject(String id) {
		String sql="SELECT id,`name`,`desc`,createtime FROM t_project WHERE id=?";
		return help.executeQueryForObject(sql, new Mapper(), id);
	}

	public void updateProject(Project project) {
		String sql="UPDATE t_project SET `name`=?,`desc`=? WHERE id=?";
		help.executeSQL(sql, project.getName(),project.getDesc(),project.getId());
	}

	public List<Project> getAllProject() {
		String sql="SELECT `id`,`name`,`desc`,`createtime` FROM `t_project`";
		return help.executeQueryForList(sql, new Mapper());
	}


}
